//
//  CZNavgationController.m
//  UIPopoverController练习
//
//  Created by chao on 15/2/27.
//  Copyright (c) 2015年 chao. All rights reserved.
//

#import "CZNavgationController.h"
#import "UIView+Frame.h"
#import "AppDelegate.h"
@interface CZNavgationController ()
@property (nonatomic, strong) NSMutableArray *images;
@property (nonatomic, strong) UIImageView *lastView;
@property (nonatomic, strong) UIView *cover;
@property (nonatomic, weak) UIView *myKeyWindow;
@end

@implementation CZNavgationController
- (NSMutableArray *)images {
    if (_images == nil) {
        _images = [NSMutableArray array];
        
    }
    return _images;
}
- (UIImageView *)lastView {
    if (_lastView == nil) {
        _lastView = [[UIImageView alloc] init];
        _lastView.frame = self.view.bounds;
//        NSLog(@"%@", [UIApplication sharedApplication].keyWindow);
//        NSLog(@"%@", self.navigationController.childViewControllers);
    }
    
    return _lastView;
}
- (UIView *)cover {
    if (_cover == nil) {
        _cover = [[UIView alloc] init];
        _cover.frame = self.view.bounds;
        _cover.backgroundColor = [UIColor grayColor];
        _cover.alpha = 0.5;

    }
    return _cover;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIPanGestureRecognizer *recongnizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
    [self.view addGestureRecognizer:recongnizer];
}
- (void)dragging:(UIPanGestureRecognizer *)recongnizer {
    // 如果是导航控制器的根控制器直接返回
    if (self.childViewControllers.count <= 1) return; {
        CGFloat tx = [recongnizer translationInView:self.view].x;
    
        if (tx < 0) return;
        if (recongnizer.state == UIGestureRecognizerStateEnded || recongnizer.state == UIGestureRecognizerStateCancelled) {
            CGFloat x = self.view.x;
            if (x >= self.view.width * 0.5) {
                [UIView animateWithDuration:0.25 animations:^{
                    self.view.transform = CGAffineTransformMakeTranslation(self.view.width, 0);
                } completion:^(BOOL finished) {
                    [self popViewControllerAnimated:NO];
                    self.view.transform = CGAffineTransformIdentity;
                }];
            } else {
                [UIView animateWithDuration:0.25 animations:^{
                    self.view.transform = CGAffineTransformIdentity;
                }];
            }
        } else {
            self.view.transform = CGAffineTransformMakeTranslation(tx, 0);
            self.lastView.image = self.images[self.images.count - 2];
                // 在popover控制器的view上插入最后一张截图 并在截图上添加蒙板
            UIView *popoverView = [self.view superview];
                [popoverView insertSubview:self.lastView atIndex:0];
                [popoverView insertSubview:self.cover aboveSubview:self.lastView];
            }

        }

    }

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    
    [self createScreenShot];
}

- (void)createScreenShot {
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [self.images addObject:image];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self createScreenShot];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    [super popViewControllerAnimated:animated];
    [self.lastView removeFromSuperview];
    [self.cover removeFromSuperview];
    [self.images removeLastObject];

    return self.childViewControllers[self.childViewControllers.count - 1];
}
@end
